﻿Imports Entities
Public Class GarantiaDAL
    Public Shared Function List() As List(Of GARANTIAS)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.garantias
                            Where rec.ACTIVO
                                            Select rec

                Return query.ToList
            End Using
        Catch ex As Exception
            Debug.Print("GarantiaDAL.List() " + ex.Message)
            Return Nothing
        End Try
    End Function

   
    Public Shared Function Create(ByVal zObject As GARANTIAS) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                bd.GARANTIAS.Add(zObject)
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Debug.Print("GarantiaDAL.Create() " + ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function Update(ByVal zObject As GARANTIAS) As GARANTIAS
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.GARANTIAS
                              Where rec.GARANTIA = zObject.GARANTIA
                              Select rec).Single

                query.CAPACIDAD = zObject.CAPACIDAD
                query.DESCRIPCION = zObject.DESCRIPCION
                If Not IsNothing(zObject.PRECIOVENTA) Then query.PRECIOVENTA = zObject.PRECIOVENTA
                query.STOCK = zObject.STOCK
                bd.SaveChanges()
                Return query
            End Using
        Catch ex As Exception
            Debug.Print("GarantiaDAL.Update() " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function Delete(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim del = (From rec In bd.GARANTIAS
                          Where rec.GARANTIA = zId
                          Select rec).Single

                bd.GARANTIAS.Remove(del)
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Debug.Print("GarantiaDAL.Delete() " + ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function Exist(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.GARANTIAS
                            Where rec.GARANTIA = zId
                            Select rec).Count
                If query > 0 Then
                    Return True
                Else
                    Return False
                End If
            End Using
        Catch ex As Exception
            Debug.Print("GarantiaDAL.Exist() " + ex.Message)
            Return False
        End Try
    End Function

    Public Shared Function SearchById(ByVal zId As Long) As GARANTIAS
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.GARANTIAS
                           Where rec.GARANTIA = zId
                           Select rec
                Return query.SingleOrDefault
            End Using
        Catch ex As Exception
            Debug.Print("GarantiaDAL.SearchById() " + ex.Message)
            Return Nothing
        End Try
    End Function

End Class
